/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProjectMoveFrame.java
 *
 * Created on 11.9.2011, 22:50:07
 */
package cz.pd.projectbuilder.main.gui.panel;

import com.jidesoft.swing.CheckBoxTree;
import com.jidesoft.swing.CheckBoxTreeSelectionModel;
import com.jidesoft.swing.SearchableUtils;
import cz.pd.projectbuilder.beans.load.ProjectBean;
import cz.pd.projectbuilder.main.util.ProjectUtils;
import java.awt.Color;
import java.awt.Component;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author forrest
 */
public class ProjectMoveFrame extends JFrame {

//  private ProjectBean chosenProjectBean;
  private Set<ProjectBean> allProjectBean = new HashSet<ProjectBean>();

  /** Creates new form ProjectMoveFrame */
  public ProjectMoveFrame() {
    initComponents();
    setSize(650, 316);
    setLocationRelativeTo(null);
    ImageIcon imageIcon = new javax.swing.ImageIcon(getClass().getResource("/icons/excel.png"));
    setIconImage(imageIcon.getImage());
    generateTabbedPaneProjekty(ProjectUtils.DAVKA.getDavky());
  }

  private void generateTabbedPaneProjekty(List<ProjectBean> projectBeans) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("All");

    for (ProjectBean projectBean : projectBeans) {
      addToTreeModel(root, projectBean);
    }

    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    addTree(treeModel);
  }

  private void addToTreeModel(DefaultMutableTreeNode root, ProjectBean projectBean) {
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(projectBean);
    root.add(treeNode);
  }

  private void addTree(DefaultTreeModel treeModel) {
    checkBoxTree.setModel(treeModel);
    checkBoxTree.setRootVisible(false);
    checkBoxTree.setShowsRootHandles(true);
    SearchableUtils.installSearchable(checkBoxTree);
    checkBoxTree.getCheckBoxTreeSelectionModel().setSelectionMode(CheckBoxTreeSelectionModel.SINGLE_TREE_SELECTION);

    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {

      @Override
      public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                                                    boolean leaf, int row, boolean hasFocus) {
        Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,
            hasFocus);
        TreePath path = tree.getPathForRow(row);
        if (((CheckBoxTree) tree).getCheckBoxTreeSelectionModel().isPathSelected(path, true)) {
          rendererComponent.setForeground(Color.BLACK);
        } else {
          rendererComponent.setForeground(Color.GRAY);
        }
        return rendererComponent;
      }
    };

    checkBoxTree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {

      @Override
      public void valueChanged(TreeSelectionEvent e) {
        TreePath[] paths = e.getPaths();
        for (TreePath path : paths) {
          ProjectBean choosenObject =
              (ProjectBean) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject();
          if (e.isAddedPath(path)) {
            allProjectBean.add(choosenObject);
          } else {
            allProjectBean.remove(choosenObject);
          }
        }
      }
    });

    checkBoxTree.setCellRenderer(renderer);
    jScrollPane1.setViewportView(checkBoxTree);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    jScrollPane1 = new javax.swing.JScrollPane();
    checkBoxTree = new com.jidesoft.swing.CheckBoxTree();
    jPanelButtons = new javax.swing.JPanel();
    jButtonUp = new javax.swing.JButton();
    jButtonDown = new javax.swing.JButton();
    jButtonEnd = new javax.swing.JButton();
    jButtonBegin = new javax.swing.JButton();

    setName("Form"); // NOI18N
    getContentPane().setLayout(new java.awt.GridBagLayout());

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    checkBoxTree.setName("checkBoxTree"); // NOI18N
    jScrollPane1.setViewportView(checkBoxTree);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
    getContentPane().add(jScrollPane1, gridBagConstraints);

    jPanelButtons.setName("jPanelButtons"); // NOI18N
    jPanelButtons.setLayout(new java.awt.GridBagLayout());

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(ProjectMoveFrame.class);
    jButtonUp.setIcon(resourceMap.getIcon("jButtonUp.icon")); // NOI18N
    jButtonUp.setText(resourceMap.getString("jButtonUp.text")); // NOI18N
    jButtonUp.setToolTipText(resourceMap.getString("jButtonUp.toolTipText")); // NOI18N
    jButtonUp.setMinimumSize(new java.awt.Dimension(30, 30));
    jButtonUp.setName("jButtonUp"); // NOI18N
    jButtonUp.setPreferredSize(new java.awt.Dimension(30, 30));
    jButtonUp.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonUpActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
    jPanelButtons.add(jButtonUp, gridBagConstraints);

    jButtonDown.setIcon(resourceMap.getIcon("jButtonDown.icon")); // NOI18N
    jButtonDown.setText(resourceMap.getString("jButtonDown.text")); // NOI18N
    jButtonDown.setToolTipText(resourceMap.getString("jButtonDown.toolTipText")); // NOI18N
    jButtonDown.setMinimumSize(new java.awt.Dimension(30, 30));
    jButtonDown.setName("jButtonDown"); // NOI18N
    jButtonDown.setPreferredSize(new java.awt.Dimension(30, 30));
    jButtonDown.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonDownActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
    jPanelButtons.add(jButtonDown, gridBagConstraints);

    jButtonEnd.setIcon(resourceMap.getIcon("jButtonEnd.icon")); // NOI18N
    jButtonEnd.setToolTipText(resourceMap.getString("jButtonEnd.toolTipText")); // NOI18N
    jButtonEnd.setMaximumSize(new java.awt.Dimension(30, 30));
    jButtonEnd.setMinimumSize(new java.awt.Dimension(30, 30));
    jButtonEnd.setName("jButtonEnd"); // NOI18N
    jButtonEnd.setPreferredSize(new java.awt.Dimension(30, 30));
    jButtonEnd.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonEndActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
    jPanelButtons.add(jButtonEnd, gridBagConstraints);

    jButtonBegin.setIcon(resourceMap.getIcon("jButtonBegin.icon")); // NOI18N
    jButtonBegin.setToolTipText(resourceMap.getString("jButtonBegin.toolTipText")); // NOI18N
    jButtonBegin.setMinimumSize(new java.awt.Dimension(30, 30));
    jButtonBegin.setName("jButtonBegin"); // NOI18N
    jButtonBegin.setPreferredSize(new java.awt.Dimension(30, 30));
    jButtonBegin.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButtonBeginActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
    jPanelButtons.add(jButtonBegin, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    getContentPane().add(jPanelButtons, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

  private void jButtonUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUpActionPerformed
    if (allProjectBean.size() == 1) {
      ProjectUtils.DAVKA.moveUp(allProjectBean.iterator().next());
      generateTabbedPaneProjekty(ProjectUtils.DAVKA.getDavky());
      ProjectUtils.setSelectedCheckBoxes(checkBoxTree, allProjectBean);
    }
  }//GEN-LAST:event_jButtonUpActionPerformed

  private void jButtonDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDownActionPerformed
    if (allProjectBean.size() == 1) {
      ProjectUtils.DAVKA.moveDown(allProjectBean.iterator().next());
      generateTabbedPaneProjekty(ProjectUtils.DAVKA.getDavky());
      ProjectUtils.setSelectedCheckBoxes(checkBoxTree, allProjectBean);
    }
  }//GEN-LAST:event_jButtonDownActionPerformed

  private void jButtonEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEndActionPerformed
    if (allProjectBean.size() == 1) {
      ProjectUtils.DAVKA.moveBottom(allProjectBean.iterator().next());
      generateTabbedPaneProjekty(ProjectUtils.DAVKA.getDavky());
      ProjectUtils.setSelectedCheckBoxes(checkBoxTree, allProjectBean);
    }
  }//GEN-LAST:event_jButtonEndActionPerformed

  private void jButtonBeginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBeginActionPerformed
    if (allProjectBean.size() == 1) {
      ProjectUtils.DAVKA.moveTop(allProjectBean.iterator().next());
      generateTabbedPaneProjekty(ProjectUtils.DAVKA.getDavky());
      ProjectUtils.setSelectedCheckBoxes(checkBoxTree, allProjectBean);
    }
  }//GEN-LAST:event_jButtonBeginActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private com.jidesoft.swing.CheckBoxTree checkBoxTree;
  private javax.swing.JButton jButtonBegin;
  private javax.swing.JButton jButtonDown;
  private javax.swing.JButton jButtonEnd;
  private javax.swing.JButton jButtonUp;
  private javax.swing.JPanel jPanelButtons;
  private javax.swing.JScrollPane jScrollPane1;
  // End of variables declaration//GEN-END:variables
}
